package com.fingard.dsp.bank.directbank.citicunion01;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IJSAPIOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqGenGetUserCodeURL;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqGenRealNameURL;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsGenGetUserCodeURL;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsGenRealNameURL;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

import java.net.URLEncoder;

public class CITICUnion01JSAPIOrder extends CITICUnionUnifiedOrder implements IJSAPIOrder {
	@Override
	public RetAtsUnifiedOrder submitJSAPIOrder(ReqUnifiedOrder p_req) {
		return submitUnifiedOrder(p_req);
	}

	@Override
	public RetAtsQueryOrder queryJSAPIPay(ReqQueryOrder p_req) {
		return queryUnifiedOrder(p_req);
	}
	
	/**
	 * 获取用户授权URL
	 *
	 */
	@Override
	public RetAtsGenGetUserCodeURL GenGetUserCodeURL(ReqGenGetUserCodeURL p_req) {
		RetAtsGenGetUserCodeURL retAts = new RetAtsGenGetUserCodeURL();
		try{
			ActSetItem actItem = this.getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "unifiedorder");
			if(actItem != null){
				String appid = p_req.appId;
				if(StringHelper.isNullOrEmpty(appid)){
					appid = actItem.appId;
				}
				String mch_id = p_req.merchantId;
				if(StringHelper.isNullOrEmpty(mch_id)){
					mch_id = actItem.merchantId;
				}

				String authUrl = bankFront.getUrl("authUrl");//微信公众号授权URL
				if(StringHelper.hasAnyChar(authUrl)){
					String authRedirectUri = bankFront.getUrl("authRedirectUri");//微信公众号授权回调地址
					if(StringHelper.hasAnyChar(authRedirectUri)){
						authUrl = authUrl+"?appid="+appid+"&redirect_uri="+java.net.URLEncoder.encode(authRedirectUri,this.getCharset())+"&response_type=code&scope=snsapi_base&state="+p_req.urid+"#wechat_redirect";
						retAts.respCode = "0";
						retAts.respInfo = "成功";
						retAts.getUserCodeURL = authUrl;
					}
					else{
						retAts.respCode = "-1";
						retAts.respInfo = "未找到授权回调地址";
					}
				}
				else{
					retAts.respCode = "-1";
					retAts.respInfo = "未找到授权URL";
				}
			}
			else{
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息";
			}
		}
		catch(Exception ex){
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return retAts;
	}

	/**
	 * 获取用户ID
	 * 
	 * 参考文档在线地址：https://docs.alipay.com/fw/api/105942
	 * 参考接口：第三步：使用auth_code换取接口access_token及用户userId
	 */
	@Override
	public RetAtsGenGetUserCodeURL getUserId(ReqGenGetUserCodeURL p_req) {
		return null;
	}

	@Override
	public RetAtsGenRealNameURL genRealNameURL(ReqGenRealNameURL p_req) {
		// TODO Auto-generated method stub
		return null;
	}
}
